package com.getword.web.controller;


import com.getword.domain.Course;
import com.getword.service.CourseService;
import com.getword.web.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/course")
public class ApiCourseController {
    @Autowired
    private CourseService service;

    @PostMapping
    public Result create(Course course){
        return service.addCourse(course);
    }
    @PutMapping
    public Result update(Course course){
        return service.updateCourse(course);
    }

    @GetMapping
    public Result query(String key, Long gradeId){
        return service.getCourseByNameAndGrade(key, gradeId);
    }

    @GetMapping("/byGradeId")
    public Result getByGradeId(Long gradeId){
        return service.getCoursesByGradeId(gradeId);
    }

    // 搜索20
    @GetMapping("/search")
    public Result search(Long gradeId, String name){
        return service.getCourseByNameAndGrade(name, gradeId);
    }

    @GetMapping("/count")
    public Result count(Course course){
        return service.getCount(course);
    }
    @GetMapping("/query")
    public Result queryAll(int page, int size, Course course){
        Pageable pageable = PageRequest.of(page -1 , size);
        return service.getCourses(pageable, course);
    }
}
